package com.clms.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clms.common.R;
import com.clms.system.mapper.RoleMapper;
import com.clms.system.pojo.entitry.SysMenu;
import com.clms.system.pojo.entitry.SysRole;
import com.clms.system.pojo.vo.OnlyoneVo;
import com.clms.system.pojo.vo.RoleVo;
import com.clms.system.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.*;

/**
 * @program: clms-third
 * @description:
 * @author: 大鱼
 * @create: 2021-07-19 18:07
 **/
@Service
public class RoleServiceimpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Override
    public R getRole(Integer userid) {
        List<SysRole> allRole = roleMapper.selectList(null);
        List<SysRole> haveRole = roleMapper.slelectbyid(userid);
        List<Integer> userhaveRole = new ArrayList<>();
        for (SysRole role : haveRole
        ) {
            userhaveRole.add((int) role.getRoleId());

        }
        return R.ok().put("roleAll", allRole).put("userRole", userhaveRole);
    }

    @Transactional
    @Override
    public R confirmRole(List<Integer> userRoleIds, Integer userId) {
        roleMapper.deleteByuserId(userId);
        roleMapper.insertrole(userRoleIds, userId);
        return R.ok();
    }

    @Transactional
    @Override
    public R initRoleTable(Integer currentPage, Integer pageSize, RoleVo roleVo) {
        Page<SysRole> page = new Page<>(currentPage, pageSize);
        roleMapper.selectList(null);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(roleVo.getRoleName())) {
            queryWrapper.like("role_name", roleVo.getRoleName());
        }
        if (StringUtils.isNotEmpty(roleVo.getStartDate())) {
            queryWrapper.ge("create_time", roleVo.getStartDate());
        }
        if (StringUtils.isNotEmpty(roleVo.getEndDate())) {
            queryWrapper.le("create_time", roleVo.getEndDate());
        }
        roleMapper.selectPage(page, queryWrapper);
        return R.ok().put("record", page.getRecords()).put("total", page.getTotal());
    }

    @Override
    public R updateRoleStatus(Integer roleId, Integer status) {
        roleMapper.updateRoleStatus(roleId, status);
        return R.ok().message("修改成功");
    }

    @Override
    public R removerole(Integer roleId) {
        roleMapper.deleteById(roleId);
        return R.ok().message("删除成功");
    }

    @Override
    public R userAddOrUpdate(SysRole role) {
        if (role.getRoleId() != 0) {
            roleMapper.updateById(role);
        } else {
            roleMapper.insert(role);
        }
        return R.ok().message("更新成功");
    }

    @Override
    public R getUserInfo(Integer roleId) {
        SysRole role = roleMapper.selectById(roleId);
        return R.ok().put("roleinfo", role);
    }

    @Override
    public R onlyOneCheckonlyOneCheckName(OnlyoneVo onlyoneVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_name", onlyoneVo.getSame());
        SysRole role = roleMapper.selectOne(queryWrapper);
        if (role == null) {
            return R.ok().put("type", true);
        } else {
            if (String.valueOf(role.getRoleId()).equals(onlyoneVo.getId())) {
                return R.ok().put("type", true);
            }
            return R.error().put("type", false);
        }
    }


}
